<template>
    <div class="crm-project-process-container">
        <el-card shadow="hover" class="mb10" v-auth="'crm:mysel:project:process:list'" v-if="!search.projectId">
            <el-form :inline="true" :model="search">
                <el-form-item>
                    <el-input v-model="search.projectName" placeholder="请输入项目名称" prefix-icon="el-icon-search"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="search.customerName" placeholder="请输入客户/企业名称" prefix-icon="el-icon-search"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input v-model="search.processName" placeholder="请输入过程名称" prefix-icon="el-icon-search"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="search.processStatus" placeholder="请选择状态" clearable class="w100">
                        <el-option label="未开始" value="0"></el-option>
                        <el-option label="进行中" value="1"></el-option>
                        <el-option label="已完成" value="2"></el-option>
                        <el-option label="已暂停" value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-date-picker type="daterange" align="right" v-model="dateData.value" :shortcuts="dateData.shortcuts" 
                        unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button icon="el-icon-search" type="primary" @click="onTableSearch()">查询</el-button>
                    <el-button icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card shadow="hover">
            <el-table ref="tableCrmProjectProcessRef" :data="tableData.data" @selection-change="onTableSelectionChange" style="width: 100%" row-key="id">
                <el-table-column type="selection" align="cneter" width="50" show-overflow-tooltip></el-table-column>
                <el-table-column type="index" align="cneter" width="50" show-overflow-tooltip></el-table-column>
                <el-table-column label="项目名称" prop="projectName" width="300" show-overflow-tooltip></el-table-column>
                <el-table-column label="客户/企业" prop="customerName" min-width="200" show-overflow-tooltip>
                    <template #default="scope">
                        <span v-if="scope.row.customerName && scope.row.enterpriseName">{{scope.row.enterpriseName}}</span>
                        <span v-if="scope.row.customerName == undefined">{{ scope.row.enterpriseName }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="过程名称" prop="processName" min-width="300" show-overflow-tooltip></el-table-column>
                <el-table-column label="开始时间" prop="processStartTime" width="180" show-overflow-tooltip></el-table-column>
                <el-table-column label="结束时间" prop="processEndTime" width="180" show-overflow-tooltip></el-table-column>
                <el-table-column label="完成时间" prop="finishTime" width="180" show-overflow-tooltip></el-table-column>
                <el-table-column label="过程状态" prop="processStatus" width="100" show-overflow-tooltip>
                    <template #default="scope">
                        <span v-if="scope.row.processStatus == '0'" class="color-info">未开始</span>
                        <span v-else-if="scope.row.processStatus == '1'" class="color-primary">进行中</span>
                        <span v-else-if="scope.row.processStatus == '2'" class="color-success">已完成</span>
                        <span v-else class="color-warning">已暂停</span>
                    </template>
                </el-table-column>
                <el-table-column label="执行人" prop="processExecutors" width="180" show-overflow-tooltip>
                    <template #default="scope">
                        <el-tag type="primary" class="mr5" v-for="executor in scope.row?.processExecutors">{{ executor.executorName }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="createTime" width="180" show-overflow-tooltip></el-table-column>
                <el-table-column label="操作" fixed="right" show-overflow-tooltip width="200" v-auths="['crm:project:process:info', 'crm:project:process:activing', 'crm:project:process:paused', 'crm:project:process:finished']">
                    <template #default="scope">
                        <el-button v-auth="'crm:project:process:info'" type="primary" link @click="onOpenInfoCrmProjectProcess(scope.row)">详情</el-button>
                        <el-button v-auth="'crm:project:process:activing'" v-show="['0','3'].includes(scope.row.processStatus)" type="primary" link @click="onOpenCrmProjectProcessStatus(scope.row, '1')">启动</el-button>
                        <el-button v-auth="'crm:project:process:paused'" v-show="['1'].includes(scope.row.processStatus)" type="primary" link @click="onOpenCrmProjectProcessStatus(scope.row, '3')">暂停</el-button>
                        <el-button v-auth="'crm:project:process:finished'" v-show="['1','3'].includes(scope.row.processStatus)" type="primary" link @click="onOpenCrmProjectProcessStatus(scope.row, '2')">完成</el-button>
                        <el-button v-auth="'crm:project:process:activing'" v-show="['2'].includes(scope.row.processStatus)" type="primary" link @click="onOpenCrmProjectProcessStatus(scope.row, '1')">重启</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                @size-change="onPageSizeChange"
                @current-change="onPageCurrentChange"
                class="mt15"
                :pager-count="5"
                :page-sizes="[10, 20, 30, 40, 50, 60]"
                v-model:current-page="search.pageNum"
                v-model:page-size="search.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="tableData.total"
            >
            </el-pagination>
        </el-card>
        <EditCrmProjectProcess ref="editCrmProjectProcessRef" @submit="onTableSearch"/>
        <InfoCrmProjectProcess ref="infoCrmProjectProcessRef"></InfoCrmProjectProcess>
    </div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted, watch } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import EditCrmProjectProcess from '/@/views/crm/project/process/editCrmProjectProcess.vue';
import InfoCrmProjectProcess from '/@/views/crm/project/process/infoCrmProjectProcess.vue';
import { editCrmProjectProcessStatus } from '/@/api/crm/project/process/index';
import { getMyselProjectProcessList } from '/@/api/crm/mysel/index';
import { formatDate, getDateRangeShortcuts } from '/@/utils/formatTime';
export default {
    name: 'crm/mysel/project/process',
    components: { InfoCrmProjectProcess, EditCrmProjectProcess },
    props: {
        project: {
            type: Object,
            default: {}
        }
    },
    setup(props, { emit }) {
        const tableCrmProjectProcessRef = ref();
        const editCrmProjectProcessRef = ref();
        const infoCrmProjectProcessRef = ref();
        const state = reactive({
            search:{
                projectId: props.project.id,
                projectName: undefined,
                customerName: undefined,
                processName: undefined,
                processStatus: undefined,
                startTime: '',
                endTime: '',
                orderBy: 'createTime',
				isAsc: 'asc',
                pageNum: 1,
                pageSize: 10,
            },
            dateData: {
                value: [],
                shortcuts: getDateRangeShortcuts()
            },
            tableData: {
                selections: [],//选中的行数据
                data: [],
                total: 0,
                loading: false
            }
        });

        const loadTableData = (params) => {
            getMyselProjectProcessList(Object.assign(state.search, params)).then((res) => {
                if(res.code == 0){
                    state.tableData.data = res.data;
                    state.tableData.total = res.total;
                    tableCrmProjectProcessRef.value.clearSelection();
                }
            });
        }

        //查询
        const onTableSearch = () => {
            if(state.dateData.value.length == 2){
                state.search.startTime = formatDate(state.dateData.value[0], 'yyyy-MM-dd') + ' 00:00:00';
                state.search.endTime = formatDate(state.dateData.value[1], 'yyyy-MM-dd') + ' 23:59:59';
            }
            loadTableData(state.search);
        }
        //重置
        const onResetSearch = () => {
            state.dateData.value = [];
            state.search.projectName = undefined;
            state.search.customerName = undefined;
            state.search.processName = undefined;
            state.search.processStatus = undefined;
            state.search.startTime = '';
            state.search.endTime = '';
            state.search.pageNum = 1;
            state.search.pageSize = 10;
            loadTableData(state.search);
        }
        // 监听表格选中行
        const onTableSelectionChange = (selections) =>{
            state.tableData.selections = selections;
        };
        // 每页显示数据大小改变
        const onPageSizeChange = (val: number) => {
            state.search.pageSize = val;
            loadTableData(state.search);
        };
        // 页码改变
        const onPageCurrentChange = (val: number) => {
            state.search.pageNum = val;
            loadTableData(state.search);
        };
        // 打开详情弹窗
        const onOpenInfoCrmProjectProcess = (row: object) => {
            row = row || state.tableData.selections[0];
            infoCrmProjectProcessRef.value.openDialog(row);
        };
        const projectStatusText = (status: String) => {
            const statusMap = {
                '0': '草稿',
                '1': '启动',
                '2': '完成',
                '3': '暂停',
            };
            return statusMap[status] || '未知';
        };

        const onOpenCrmProjectProcessStatus = (row: object, status: string) => {
            if('2' == status){
                row.operate = 'finished';
                editCrmProjectProcessRef.value.openDialog(row);
            }else{
                ElMessageBox.confirm(`确认【${ projectStatusText(status) }】操作吗？`, '提示', {
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    type: 'warning',
                }).then(() => {
                    editCrmProjectProcessStatus({ id: row.id, status: status }).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('操作成功');
                            loadTableData({});
                        }
                    });
                }).catch(() => {});
            }
        };
        // 页面加载时
        onMounted(() => {
            loadTableData({});
        });

        watch(() => props.project, (project) => {
            state.search.projectId = project.id;
            loadTableData({});
        },{deep: true})

        return {
            tableCrmProjectProcessRef,
            editCrmProjectProcessRef,
            infoCrmProjectProcessRef,
            onTableSearch,
            onResetSearch,
            onTableSelectionChange,
            onPageSizeChange,
            onPageCurrentChange,
            onOpenInfoCrmProjectProcess,
            onOpenCrmProjectProcessStatus,
            ...toRefs(state),
        };
    },
};
</script>